<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right"
                   style="font-size: 18px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增专业</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="专业名称" prop="majorName">
        <el-input v-model="ruleForm.majorName"></el-input>
      </el-form-item>
      <el-form-item label="成立时间" prop="establishedTime">
        <template>
          <el-date-picker
                  v-model="ruleForm.establishedTime"
                  align="right"
                  type="date"
                  placeholder="选择日期">
          </el-date-picker>
        </template>
      </el-form-item>
      <el-form-item label="专业人数" prop="num">
        <el-input v-model="ruleForm.num"></el-input>
      </el-form-item>
      <el-form-item label="负责老师" prop="teacherName">
        <el-select v-model="ruleForm.teacherId" placeholder="请选择">
          <el-option
              v-for="item in teacherListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属院系" prop="facultyId">
        <el-select v-model="ruleForm.facultyId" placeholder="请选择">
          <el-option
                  v-for="item in facultyListOptions"
                  :key="item.id"
                  :label="item.facultyName"
                  :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">新增专业</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        facultyListOptions:[],
        teacherListOptions: [],
        ruleForm: {
          majorName: '',
          establishedTime: '',
          num: '',
          teacherId: [],
          name:'',
          facultyId: [],
          facultyName: ''
        },
        rules: {
          majorName: [
            { required: true, message: '请输入专业名称', trigger: 'blur' },
          ],
          establishedTime: [
            { required: true, message: '请选择成立时间', trigger: 'blur' },
          ],
          num: [
            { required: true, message: '请输入学生人数', trigger: 'blur' },
          ],
          teacherName: [
            { required: false, message: '请选择负责老师', trigger: 'blur' },
          ],
          facultyId: [
            { required: true, message: '请选择所属院系', trigger: 'blur' },
          ]
        }
      };
    },
    methods: {
      loadFacultyList() {
        console.log('loadRoFacultyList');
        let url = 'http://localhost:9801/faculties';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.facultyListOptions = responseBody.data;
          console.log(this.facultyListOptions);
          console.log();
        });
      },
      loadTeacherList() {
        console.log('loadTeacherList');
        let url = 'http://localhost:9800/teacher';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.teacherListOptions = responseBody.data;
        });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {  /*对表单进行检查验证*/
          if (valid) {
            let url = 'http://localhost:9801/majors/add-new';
            console.log('url=' + url);
            console.log(this.ruleForm);
            let formData = this.qs.stringify(this.ruleForm);
            console.log('formData = ' + formData);
            this.axios.post(url, formData).then((response) => {
              let responseBody = response.data;
              console.log('responseBody = ');
              console.log(responseBody);
              if (responseBody.state == 20000){
                this.$message({
                  message: "添加专业成功",
                  type: 'success'
                });
                this.resetForm(formName);
              } else {
                this.$message.error(responseBody.message);
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    mounted() {
      this.loadFacultyList();
      this.loadTeacherList();
    }
  }
</script>
